package com.yandadeyu.springBoot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 创建数据源 primaryDatasource  secondaryDataSource
 * Created by Administrator on 2018/6/19.
 */
@Configuration
@PropertySource({ "classpath:application.properties" })
public class DataSourceConfig {
    @Bean
    @Primary
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.primary.datasource")
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }
  /*  @Autowired
    private Environment env;
    @Bean
    @Qualifier("primaryDataSource")
    @Primary
    public DataSource primaryDataSource() {
        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                env.getProperty("spring.primary.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.primary.datasource.jdbc-url"));
        dataSource.setUsername(env.getProperty("spring.primary.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.primary.datasource.password"));

        return dataSource;
    }*/
    @Bean
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.secondary.datasource")
    public DataSource secondaryDataSource(){
        return DataSourceBuilder.create().build();
    }
    /*@Bean
    @Qualifier("secondaryDataSource")
    public DataSource secondaryDataSource() {
        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                env.getProperty("spring.secondary.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.secondary.datasource.jdbc-url"));
        dataSource.setUsername(env.getProperty("spring.secondary.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.secondary.datasource.password"));

        return dataSource;
    }*/
}
